﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;

namespace BestBuy.Account
{
    public partial class Login : System.Web.UI.Page
    {
        private CThongTinCaNhan _cttcn = new CThongTinCaNhan();
        private CNhanVien _cnv = new CNhanVien();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.UrlReferrer != null && !Request.UrlReferrer.PathAndQuery.Equals("/PageGuest/PageRegister.aspx"))
                {
                    this.ViewState["returnurl"] = Request.UrlReferrer.PathAndQuery;
                }
                else {
                    this.ViewState["returnurl"] = "/";
                }
            }
            pnThongBao.Visible = false;
            RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {

            string email = LoginUser.UserName.ToString();
            string matkhau = LoginUser.Password.ToString();
            ThongTinTaiKhoan tttk = _cttcn.CheckLogin(email, matkhau);
            string DuongDan = "";
            if (tttk != null)
            {
                Session["TenDN"] = tttk.HoTen;
                Session["LoaiTK"] = tttk.LoaiND;
                Session["IDDN"] = tttk.IDTaiKhoan;
                if (Request.Params["ml"] != null && Request.Params["pid"] != null)
                {
                    try
                    {
                        int ml = Convert.ToInt32(Request.Params["ml"].ToString());
                        int pid = Convert.ToInt32(Request.Params["pid"].ToString());
                        DuongDan = "~/PageGuest/PageContactMessage.aspx?ml=" + Request.Params["ml"].ToString() + "&pid=" + Request.Params["pid"].ToString();
                    }
                    catch
                    {
                        Response.Redirect("~/PageGuest/PageError.aspx");
                    }
                }
                else
                {
                    DuongDan = this.ViewState["returnurl"].ToString();
                }
                
            }
            else {//kiem tra bang nhan vien
                NhanVien nv = _cnv.CheckLogin(email, matkhau);
                if (nv != null)
                {
                    Session["TenDN"] = nv.HoTen;
                    Session["LoaiTK"] = nv.LoaiND;
                    Session["IDDN"] = nv.IDTaiKhoan;
                    if (Request.Params["ml"] != null && Request.Params["pid"] != null)
                    {
                        try
                        {
                            int ml = Convert.ToInt32(Request.Params["ml"].ToString());
                            int pid = Convert.ToInt32(Request.Params["pid"].ToString());
                            DuongDan = "~/PageGuest/PageContactMessage.aspx?ml=" + Request.Params["ml"].ToString() + "&pid=" + Request.Params["pid"].ToString();
                        }
                        catch
                        {
                            Response.Redirect("~/PageGuest/PageError.aspx");
                        }
                    }
                    else
                    {
                        DuongDan = this.ViewState["returnurl"].ToString();
                    }
                }
                else
                {
                    pnThongBao.Visible = true;
                    pnThongBao.CssClass = "InLeft error message";
                    return;
                    
                }
                
            }
            Response.Redirect(DuongDan); 
        }
    }
}
